package droid.insane.logic;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author Sandamal
 *A custom ListAdapter to be used in lists
 */
public class ExtendedListView extends BaseAdapter {

	private ArrayList<String[]> data;
	private LayoutInflater mInflater;

	public ExtendedListView(Context context, ArrayList<String[]> items,
			int textViewResourceId) {

		// Cache the LayoutInflate to avoid asking for a new one each time.
		mInflater = LayoutInflater.from(context);
		data = items;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listentry, null);
			holder = new ViewHolder();
			holder.symbol = (TextView) convertView.findViewById(R.id.txtv_Col1);
			holder.price = (TextView) convertView.findViewById(R.id.txtv_Col2);
			holder.changeRs = (TextView) convertView
					.findViewById(R.id.txtv_Col3);
			holder.changePrec = (TextView) convertView
					.findViewById(R.id.txtv_Col4);
			holder.compName = (TextView) convertView
					.findViewById(R.id.txtv_Col1Sub1);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		String[] strArr = data.get(position);

		if (strArr != null) {

			holder.symbol.setText(strArr[0]);
			holder.compName.setText(strArr[1]);
			holder.price.setText(strArr[2]);
			holder.changeRs.setText(strArr[3]);

			if (strArr[4].length() != 0) {

				if (strArr[4].startsWith("-")) {
					holder.changePrec.setTextColor(Color.RED);
					holder.changeRs.setTextColor(Color.RED);
					holder.changePrec.setText("(" + strArr[4] + "%)");
				} else {
					holder.changePrec.setTextColor(Color.GREEN);
					holder.changeRs.setTextColor(Color.GREEN);
					holder.changePrec.setText(strArr[4] + "%");
				}
			}

		}
		return convertView;
	}

	/*
	 * The number of items in the list is determined by the number of companies
	 * in the arraylist
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return data.size();
	}

	/*
	 * Since the data comes from an array, just returning the index is sufficent
	 * to get at the data.
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		return position;
	}

	/*
	 * Use the array index as a unique id.
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	static class ViewHolder {
		TextView symbol;
		TextView price;
		TextView changeRs;
		TextView changePrec;
		TextView compName;
	}

	public void updateDataset(ArrayList<String[]> newData) {
		this.data = newData;

	}

}